***********************************************************************************
********"Macroprudential Regulation, Quantitative Easing, and Bank Lending"********
***********************************************************************************
* This file sets several datasets that will be used for the analysis of monetary 
* policy shocks between 2010 and 2020
***********************************************************************************
***********************************************************************************


*==================================================================================
**# Bookmark (input)
clear *
use $inputdirectory/typeover, clear

foreach v of varlist _all {
capture rename `v' `=lower("`v'")'
}

replace description="OPERATORI MICROCREDITO" if type==69

rename abi bankid
sort bankid
labmask type, values(description)
drop description

**# Bookmark (saving dataset - bank type - e.g. joint stock company, mutual bank)
compress
save $inputdirectory/INB_typesmonpol, replace


*==================================================================================
**# Bookmark (input)
clear *
use $inputdirectory/gruover, clear

foreach v of varlist _all {
capture rename `v' `=lower("`v'")'
}

rename ente_segn bankid
duplicates drop gru bankid year, force

**# Bookmark (saving dataset - map of the groups)
compress
save $inputdirectory/INB_grumonpol, replace


*==================================================================================
**# Boomark (input)
clear *
import sas using $inputdirectory/assets, case(lower)

rename dat time
tostring(time), replace
gen xxx=date(time, "YMD")
gen year=year(xxx)
rename time oldtime
gen time=mofd(xxx)
drop xxx
format time %tm
format assets %20.0g
rename ente_segn bankid
drop oldtime

merge m:1 bankid year using $inputdirectory/INB_typesmonpol, keepus(type)
keep if _merge==3
drop _merge

merge m:1 bankid year using $inputdirectory/INB_grumonpol, keepus(gru)
keep if _merge!=2
drop _merge
replace gru=bankid if gru==.

gen xxx=type if bankid==gru
egen grutype=max(xxx), by(gru)
replace grutype=10 if grutype==.
drop xxx
keep if grutype==10

drop bankid
rename gru bankid

keep if time>=tm(2009m12)&time<=tm(2020m2)
collapse (sum) assets, by(bankid time)

**# Bookmark (saving dataset - total assets)
compress
save $inputdirectory/INB_assetsmonpol, replace


*==================================================================================
**# Boomark (input)
clear *
import sas using $inputdirectory/refin
rename *, lower

rename dat time
tostring(time), replace
gen xxx=date(time, "YMD")
gen year=year(xxx)
rename time oldtime
gen time=mofd(xxx)
drop xxx
format time %tm
rename ente_segn bankid
rename rifinbil refin
drop oldtime

merge m:1 bankid year using $inputdirectory/INB_typesmonpol, keepus(type)
keep if _merge==3
drop _merge

merge m:1 bankid year using $inputdirectory/INB_grumonpol, keepus(gru)
keep if _merge!=2
drop _merge
replace gru=bankid if gru==.

gen xxx=type if bankid==gru
egen grutype=max(xxx), by(gru)
replace grutype=10 if grutype==.
drop xxx
keep if grutype==10

drop bankid
rename gru bankid

keep if time>=tm(2009m12)&time<=tm(2020m2)
collapse (sum) refin, by(bankid time)
xtset bankid time
gen lncrefin=100*(ln(refin+1)-ln(L.refin+1))
keep bankid lncrefin time

**# Boomark (saving dataset - borrowing from the ECB)
compress
save $inputdirectory/INB_refinmonpol, replace


*==================================================================================
**# Boomark (input)
clear *
import sas using $inputdirectory/ecbdepo
rename *, lower

rename dat time
tostring(time), replace
gen xxx=date(time, "YMD")
gen year=year(xxx)
rename time oldtime
gen time=mofd(xxx)
drop xxx
format time %tm
rename ente_segn bankid
drop oldtime

merge m:1 bankid year using $inputdirectory/INB_typesmonpol, keepus(type)
keep if _merge==3
drop _merge

merge m:1 bankid year using $inputdirectory/INB_grumonpol, keepus(gru)
keep if _merge!=2
drop _merge
replace gru=bankid if gru==.

gen xxx=type if bankid==gru
egen grutype=max(xxx), by(gru)
replace grutype=10 if grutype==.
drop xxx
keep if grutype==10

drop bankid
rename gru bankid

keep if time>=tm(2009m12)&time<=tm(2020m2)
collapse (sum) ecbdepo, by(bankid time)
keep bankid ecbdepo time

**# Boomark (saving dataset - deposits at the ECB)
compress
save $inputdirectory/INB_ecbdepomonpol, replace


*==================================================================================
**# Boomark (input)
clear *
use $inputdirectory/cashover, clear

rename *, lower
tostring(time), replace
gen xxx=date(time, "YMD")
gen year=year(xxx)
rename time oldtime
gen time=mofd(xxx)
drop xxx
format time %tm
drop oldtime

merge m:1 bankid year using $inputdirectory/INB_typesmonpol, keepus(type)
keep if _merge==3
drop _merge

merge m:1 bankid year using $inputdirectory/INB_grumonpol, keepus(gru)
keep if _merge!=2
drop _merge
replace gru=bankid if gru==.

gen xxx=type if bankid==gru
egen grutype=max(xxx), by(gru)
replace grutype=10 if grutype==.
drop xxx
keep if grutype==10

drop bankid
rename gru bankid

keep if time>=tm(2009m12)&time<=tm(2020m2)
collapse (sum) cash, by(bankid time)

**# Boomark (saving dataset - cash)
compress
save $inputdirectory/INB_cashmonpol, replace
